#include<iostream>
#include<pthread.h>
#include<unistd.h>


int cnt = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* Count(void* args)
{
    pthread_detach(pthread_self());
    uint64_t num = (uint64_t)args;
    std::cout<<"pthread: "<<num<<" create success!"<<std::endl;

    while(true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond, &mutex); // 先加锁，后等待
        std::cout<<"pthread: "<<num<<", cnt: "<<cnt++<<std::endl;
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
    for(uint64_t i = 0; i < 5; i++)
    {
        pthread_t tid;
        pthread_create(&tid, nullptr, Count, (void*)i); // 传递 i，直接拷贝，防止变为临界资源
        usleep(1000);
    }
    sleep(3);
    // 开始控制
    std::cout<<"main thread control begin: "<<std::endl;

    while(true)
    {
        sleep(1);
        //pthread_cond_signal(&cond);
        pthread_cond_broadcast(&cond);
        std::cout<<"signal a thread..."<<std::endl;
    }

    return 0;
}